package com.cqeec.myblog.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.Article;
import com.cqeec.myblog.model.Category;
import com.cqeec.myblog.service.ArticleService;
import com.cqeec.myblog.service.ArticleServiceImpl;
import com.cqeec.myblog.service.CategoryService;
import com.cqeec.myblog.service.CategoryServiceImpl;
import com.cqeec.myblog.util.PageBean;

/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet("/articleServlet")
public class ArticleServlet extends HttpServlet {
    ArticleService articleService=new ArticleServiceImpl();	
    CategoryService categoryService=new CategoryServiceImpl();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation=(String)request.getParameter("operation");
		String articleId=(String)request.getParameter("articleId");
		//根据文章id删除数据库指定记录
		if("delete".equals(operation)) {
			articleService.deleteArticleById(articleId);
		}
		//跳转到跟新文章页面
		else if("updatePage".equals(operation)){
			Article article=articleService.getArticleById(articleId);
			request.setAttribute("content", article.getContent());
			request.setAttribute("title", article.getTitle());
			request.setAttribute("articleId", article.getId());
			request.getRequestDispatcher("/edit.jsp").forward(request, response);
		}
		//接收客户端的消息，更新数据库文章记录
		else if ("update".equals(operation)) {
             String content=(String) request.getParameter("content");
             String title=(String) request.getParameter("title");
             String id=(String) request.getParameter("articleId");
             articleService.updateById(id,title,content);
		}
		//根据文章id获取到全文内容，并显示到相应页面
		else if("readArticle".equals(operation)) {
			Article article=articleService.getArticleById(articleId);
			request.setAttribute("content", article.getContent());
			request.getRequestDispatcher("/content.jsp").forward(request, response);
		}
		//转发至创建文章页面
		else if("createPage".equals(operation)){
            List<Category> list=categoryService.getAllCategory();            		
			request.setAttribute("list", list);
			request.getRequestDispatcher("/create.jsp").forward(request, response);			
		}
		//根据客户端传来的数据创建文章
		else if("createArticle".equals(operation)) {
              String nick=request.getParameter("nick");
              String title=request.getParameter("title");
              String content=request.getParameter("content");
              String categoryId=request.getParameter("categoryId");
              Article article=new Article();
              article.setCreationTime(new Date());
              article.setTitle(title);
              article.setContent(content);
              article.setBlogger(nick);
              article.setId(UUID.randomUUID().toString());
              article.setCategoryId(categoryId);
              articleService.createArticle(article);              
		}
		//分页查询所有文章的详情
		else if("findArticleDescribByCondition".equals(operation)) {
			//获取条件
			String  cid=request.getParameter("cid");
			String  strCurPage=request.getParameter("curPage");
			Integer curPage=null;
			if(strCurPage==null) {
				curPage=1;
			}else {
				curPage=Integer.parseInt(strCurPage);
			}
			PageBean<Article> pageBean=articleService.findArticleDescribByCondition(cid,curPage);
			
			//计算数组按钮的值
			pageBean.calculationNumBtn();
			//生成数字按钮的超链接
			pageBean.setUrl("http://localhost:8080/MyBlog/articleServlet?operation=findArticleDescribByCondition");
			//分类参数封装
			if(cid!=null&&cid!="") {
				pageBean.getData().put("cid", cid);
			}
			//获取到所有分类
			List<Category> list=categoryService.getAllCategory();
			pageBean.getData().put("categoryList", list);
			
			
			//传递参数到页面
			request.setAttribute("pageBean",pageBean);
			//转发到jsp页面中
			request.getRequestDispatcher("/articleCategoryDescrib.jsp").forward(request, response);
			
		}
		//如果以上都不匹配则返回一个错误页面
		else{
			response.sendRedirect("/MyBlog/error/requestError.jsp");
			return;
		}
		
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
